package com.qiaoyanqing.ximalaya.discovermodule.discover_activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.DiscoverRankSecondSingleAdapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.DiscoverRankSecondService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRankSecondDataEntity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRankSecondEntity;
import com.qiaoyanqing.ximalaya.url.Urlconfig;
import com.qiaoyanqing.ximalaya.util.MyLog;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DiscoverRankSecondSingleActivity extends AppCompatActivity implements AbsListView.OnScrollListener, Callback<DiscoverRankSecondEntity>, AdapterView.OnItemClickListener {
    private DiscoverRankSecondSingleAdapter adapter;
    private List<DiscoverRankSecondDataEntity> list;
    private String contentType;
    private String key;
    private int pageSize=20;
    private String title;
    private TextView textView;
    private ListView listView;
    private int pageId=1;
    private boolean isBottom=false;
    private SwipeRefreshLayout swipeRefreshLayout;
    private boolean isLoadMore;
    private int totalCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discover_rank_second_single);
        initView();
    }

    private void initView() {

        listView=(ListView)findViewById(R.id.rank_single_list_view);
        textView=(TextView)findViewById(R.id.title);
        list=new ArrayList<>();
        Intent intent=getIntent();
        title=intent.getStringExtra("title");
        textView.setText(title);
        contentType=intent.getStringExtra("contentType");
        key=intent.getStringExtra("key");
        loadDatas(pageSize);
        adapter=new DiscoverRankSecondSingleAdapter(this,list,contentType);
        listView.setAdapter(adapter);
        listView.setOnScrollListener(this);
        listView.setOnItemClickListener(this);
    }
    public void loadDatas(int pageSize){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(Urlconfig.RANK_SECOND_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        DiscoverRankSecondService service=retrofit.create(DiscoverRankSecondService.class);

        service.getDatas(contentType,"android",key,1,pageSize,2).enqueue(this);

    }

    @Override
    public void onResponse(Call<DiscoverRankSecondEntity> call, Response<DiscoverRankSecondEntity> response) {
        String title=response.body().getTitle();
        MyLog.i("SSSSSSSSSS",title);
        list.removeAll(list);
        list.addAll(response.body().getList());
        adapter.notifyDataSetChanged();
        // swipeRefreshLayout.setRefreshing(false);
        //isLoadMore=true;
    }

    @Override
    public void onFailure(Call<DiscoverRankSecondEntity> call, Throwable t) {

    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        if(isBottom&&i==SCROLL_STATE_IDLE&&pageSize<100){
            pageSize+=20;
            loadDatas(pageSize);
        }
    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {
        if(i+i1==i2){
            isBottom=true;
        }else {
            isBottom=false;
        }
    }
    public void onClick(View view){
        finish();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (contentType){
            case "track":
                play();
                break;
            case "album":
                break;
        }
    }

    private void play() {
        Intent intent=new Intent(this,DiscoverRankPlayActivity.class);
        startActivity(intent);
    }
}
